﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Utility;
using Utility.Config;
using Utility.Json;
using Utility.Models;
using Utility.Pay.Alipay;
using Utility.Pay.Wxpay;

namespace JYJJ.Web.Pages
{
    public partial class payQR : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string strOrderID = ParamsOfRequest.Request("orderId");
            model_Donate_Order order = JYJJ.BLL.ServceHelper.ServiceDonate().GetDonateOrderModelByOrderID(strOrderID);
            if (order != null)
            {
                txtTrano.Value = order.orderId;
                donateId.Value = order.donateId.ToSafeString();
                lblTrano.InnerText = order.orderId;
                lbOrderType.InnerText = ConfigHelper.subject;
                lblFee.InnerText = order.payMoney.ToSafeString();
                hidPayType.Value = order.way.ToSafeString();
                if (order.way == 0)
                {
                    #region 支付宝支付
                    AliPay ali = new AliPay();
                    ali.tradeNo = order.orderId;
                    ali.subject = ConfigHelper.subject;
                    ali.body = order.orderId + "|" + order.Id.ToSafeString() + "|" + order.donateId.ToSafeString();
                    ali.price = order.payMoney.ToSafeString();
                    ali.notifyUrl = ConfigHelper.notify_url;
                    ////////////////////////////////////////////请求参数////////////////////////////////////////////
                    //支付类型
                    string payment_type = "1";
                    //必填，不能修改
                    //服务器异步通知页面路径
                    string notify_url = ConfigHelper.notify_url;
                    //需http://格式的完整路径，不能加?id=123这类自定义参数

                    //页面跳转同步通知页面路径
                    string return_url = ConfigHelper.return_url;
                    //需http://格式的完整路径，不能加?id=123这类自定义参数，不能写成http://localhost/
                    //商户订单号
                    string out_trade_no = ali.tradeNo;
                    //商户网站订单系统中唯一订单号，必填
                    //订单名称
                    string subject = ConfigHelper.subject;
                    //必填
                    //付款金额
                    string total_fee = ali.price;
                    //必填
                    //订单描述
                    string body = ali.body;
                    //商品展示地址
                    string show_url = "";//需以http://开头的完整路径，例如：http://www.商户网址.com/myorder.html

                    //防钓鱼时间戳
                    string anti_phishing_key = "";
                    //若要使用请调用类文件submit中的query_timestamp函数

                    //客户端的IP地址
                    string exter_invoke_ip = "";
                    //非局域网的外网IP地址，如：221.0.0.1


                    ////////////////////////////////////////////////////////////////////////////////////////////////

                    //把请求参数打包成数组
                    SortedDictionary<string, string> sParaTemp = new SortedDictionary<string, string>();
                    sParaTemp.Add("partner", Config.Partner);
                    sParaTemp.Add("seller_email", Config.Seller_email);
                    sParaTemp.Add("_input_charset", Config.Input_charset.ToLower());
                    sParaTemp.Add("service", "create_direct_pay_by_user");
                    sParaTemp.Add("payment_type", payment_type);
                    sParaTemp.Add("notify_url", notify_url);
                    sParaTemp.Add("return_url", return_url);
                    sParaTemp.Add("out_trade_no", out_trade_no);
                    sParaTemp.Add("subject", subject);
                    sParaTemp.Add("total_fee", total_fee);
                    sParaTemp.Add("body", body);
                    sParaTemp.Add("show_url", show_url);
                    sParaTemp.Add("anti_phishing_key", anti_phishing_key);
                    sParaTemp.Add("exter_invoke_ip", exter_invoke_ip);

                    //Session["out_trade_no"] = out_trade_no;

                    //建立请求
                    string sHtmlText = Submit.BuildRequest(sParaTemp, "get", "确认");
                    Response.Write(sHtmlText);
                    #endregion
                }
                else if (order.way == 1)
                {
                    #region 微信支付
                    WeChatPay objwx = new WeChatPay();
                    objwx.out_trade_no = order.orderId;
                    objwx.body = ConfigHelper.subject;
                    objwx.total_fee = order.payMoney.ToSafeString();
                    objwx.product_id = order.orderId + "|" + order.Id.ToSafeString() + "|" + order.donateId.ToSafeString();
                    NativePay nativePay = new NativePay();
                    //生成扫码支付模式二url
                    string url1 = nativePay.GetPayUrl_New(objwx.out_trade_no, objwx.body, objwx.total_fee, objwx.product_id);
                    if (!url1.IsNullOrEmpty())
                    {
                        string url = System.Web.HttpUtility.UrlEncode(url1, System.Text.Encoding.UTF8);
                        //string tradeno = System.Web.HttpUtility.UrlEncode(objwx.out_trade_no, System.Text.Encoding.UTF8);
                        //string totalfee = System.Web.HttpUtility.UrlEncode(objwx.total_fee, System.Text.Encoding.UTF8);
                        imgWx.ImageUrl = "MakeQRCode.aspx?data=" + url;
                    }
                    else
                    {
                        string script = " alert('提示：生成支付信息出现异常，请重新提交支付');   window.location.href='detail.aspx?id=" + donateId + "';";
                        HttpContext.Current.Response.Write("<script language=\"javascript\" type=\"text/javascript\" defer>\n" + script + "\n</script>\n");
                        HttpContext.Current.Response.End();
                    }
                    #endregion
                }
            }
        }
    }
}